package org.bitejiuyeke;


import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;
import java.text.MessageFormat;
import java.util.Scanner;

/**
 * @Author 比特就业课
 */

public class Demo03_DataSource {
    public static void main(String[] args) {
        // 定义MYSQL数据源对象
        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        // 设置数据库连接串
        mysqlDataSource.setURL("jdbc:mysql://127.0.0.1:3306/java114?characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false");
        // 用户名
        mysqlDataSource.setUser("root");
        // 密码
        mysqlDataSource.setPassword("123456");

        // 定义JDBC的数据源对象
        DataSource dataSource = mysqlDataSource;

        // 定义数据库连接对象
        Connection connection = null;
        // 定义预处理SQL执行对象
        PreparedStatement statement = null;
        // 定义结果集对象
        ResultSet resultSet = null;

        try {
            // 1. 通过数据源对象获取数据库连接
            connection = dataSource.getConnection();
            // 定义SQL语
            String sql = "select id, name, sno, age, gender, enroll_date, class_id from student where name = ?";
            // 2. 预处理SQL执行对象
            statement = connection.prepareStatement(sql);
            // 接收用户的输入
            System.out.println("请输入学生姓名:");
            Scanner scanner = new Scanner(System.in);
            String inName = scanner.next();
            // 3. 用真实的值替换占位符
            statement.setString(1, inName);
            // 4. 执行SQL 获取结果
            resultSet = statement.executeQuery();
            // 5. 遍历结果集
            // 6. 遍历结果集，获取数据行
            while (resultSet.next()) {
                // 获取ID列的值
                long id = resultSet.getLong(1);
                // 获取name列的值
                String name = resultSet.getString(2);
                String sno = resultSet.getString(3);
                int age = resultSet.getInt(4);
                byte gender = resultSet.getByte(5);
                Date enrollDate = resultSet.getDate(6);
                long classId = resultSet.getLong(7);
                System.out.println(MessageFormat.format("学生编号={0}, 姓名={1}, 学号={2}, 年龄={3}, 性别={4}, 入学时间={5}, " +
                        "班级编号={6}", id, name, sno, age, gender, enrollDate, classId));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 释放结果集对象
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            // 释放Statement
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            // 关闭数据库连接
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
